home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / float.c < prev    next >
C/C++ Source or Header  |  1990-04-12  |  4KB  |  134 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /*  WAM representation of floats:
  25.  *     bits 0-2 : tag (010);
  26.  *     bits 3-7 : absolute value of exponent;
  27.  *     bits 8-28: absolute value of mantissa;
  28.  *     bit    29: sign of exponent (1: negative);
  29.  *     bit    30: sign of mantissa (1: negative).
  30. */
  31.  
  32. #include "sim.h"
  33. #include "aux.h"
  34.  
  35. #define Bit20          0x100000
  36. #define Bits15to19     0x0f8000
  37.  
  38. #define MIN(x, y)      (x > y ? y : x)
  39.  
  40. #define EXP_SIGN       0x20000000
  41. #define MANT_SIGN      0x40000000
  42. #define EXP_MAGN(op)   (((unsigned)(op & 0xf8)) >> 3)
  43. #define MANT_MAGN(op)  (((unsigned)(op & 0x1fffff00)) >> 8)
  44.  
  45. double frexp(), ldexp();   /* C library routines */
  46.  
  47. /* "floatval" converts floats from the WAM representation to
  48.  *            the machine representation.
  49. */
  50. double floatval(op)
  51. LONG op;
  52. {
  53.    LONG   exponent, exp;
  54.    double fval;
  55.  
  56.    fval = (double)MANT_MAGN(op);
  57.    exponent = EXP_MAGN(op);
  58.    exp = (op & EXP_SIGN) ? -exponent : exponent;
  59.    if (op & MANT_SIGN)
  60.       fval = -fval;
  61.    fval = ldexp(fval, exp);
  62.    return fval;
  63. }
  64.  
  65. /* "makefloat" converts floats from the machine representation
  66.  *             to the WAM representation.
  67. */
  68. LONG makefloat(op)
  69. double op;
  70. {
  71.    LONG         exp_sign, mant_sign, int_op;
  72.    int          exponent, nshift;
  73.    WORD         num_gaps;
  74.    unsigned int mask;
  75.  
  76.    if (op < 0) {
  77.       mant_sign = MANT_SIGN;
  78.       op = -op;
  79.    }
  80.    else mant_sign = 0;
  81.    op = frexp(op, &exponent);
  82.    if (op == 0.0 || exponent <= -32) {
  83.       int_op = 0;
  84.       exponent = 0;
  85.    } else {
  86.       while(!((int_op = (int)(op + 0.5)) & Bit20) && exponent > -31) {
  87.          /* keep top 10 bits clear for shifting */
  88.          mask = (int_op & Bits15to19) >> 15;
  89.          switch (mask) {
  90.             case  0: num_gaps = 6;  break;
  91.             case  1: num_gaps = 5;  break;
  92.             case  2:
  93.             case  3: num_gaps = 4;  break;
  94.             case  4:
  95.             case  5:
  96.             case  6:
  97.             case  7: num_gaps = 3;  break;
  98.             case  8:
  99.             case  9:
  100.             case 10:
  101.             case 11:
  102.             case 12:
  103.             case 13:
  104.             case 14:
  105.             case 15: num_gaps = 2;  break;
  106.             default: /* cases 16 to 31 */
  107.                      num_gaps = 1;  break;
  108.          }
  109.          nshift = MIN(exponent - (-31), num_gaps);
  110.          switch (nshift) {
  111.             case 0 : break;
  112.             case 1 : op *= 2;   break;
  113.             case 2 : op *= 4;   break;
  114.             case 3 : op *= 8;   break;
  115.             case 4 : op *= 16;  break;
  116.             case 5 : op *= 32;  break;
  117.             case 6 : op *= 64;  break;
  118.             default: printf("makefloat: unexpected multiplier %d\n", nshift);
  119.          }
  120.          exponent -= nshift;
  121.       }  /* while */
  122.    }  /* else */
  123.  
  124.    if (exponent < 0) {
  125.       exponent = -exponent;
  126.       exp_sign = EXP_SIGN;
  127.    } else exp_sign = 0;
  128.  
  129.    return (((LONG)int_op << 8) | (exponent << 3) |
  130.        exp_sign | mant_sign | FLOAT_TAG);
  131. }
  132.  
  133.  
  134.